package com.igor.bookStore.tapestry.pages;

import java.util.Collection;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.ioc.annotations.Inject; 

import com.igor.bookStore.asobject.PurchaseItemBasketAS;
import com.igor.bookStore.beans.CustomerBean;
import com.igor.bookStore.beans.PurchaseBean;
import com.igor.bookStore.inface.Book;
import com.igor.bookStore.inface.Customer;
import com.igor.bookStore.inface.Purchase;
import com.igor.bookStore.inface.PurchaseItem;
import com.igor.bookStore.inface.Seller;
import com.igor.bookStore.inface.User;
import com.igor.bookStore.service.*;

public class EntryPurchase {

	  @ApplicationState
      private PurchaseItemBasketAS purchaseItemBasketAS;
      
      private Book singleBook;
      private PurchaseItem singlePurchaseItemFromBasket;
      
      @InjectPage
      private ViewPurchase viewPurchasePage;
      @InjectPage
      private ViewError viewErrorPage;
      
      @Inject
      private Books books;
      @Inject
      private Customers customers;
      @Inject
      private Sellers sellers;
      @Inject
      private Purchases purchases;
      @Inject
      private PurchaseItems purchasesItems;

      @ApplicationState
      private User user;
      private boolean userExists;
      public  User getUser()  {
              return this.user;
      }

      public Collection<PurchaseItem> getPurchaseItemBasket() {
              return purchaseItemBasketAS.getPurchaseItemBasket().values();
      }
      public PurchaseItem getSinglePurchaseItemFromBasket() {
              return singlePurchaseItemFromBasket;
      }
      public void setSinglePurchaseItemFromBasket(PurchaseItem singlePurchaseItemFromBasket) {
              this.singlePurchaseItemFromBasket = singlePurchaseItemFromBasket;
      }

      public Collection<Book> getBooks() {
              return books.retrieve();
      }
      public Book getSingleBook() {
              return singleBook;
      }
      public void setSingleBook(Book singleBook) {
              this.singleBook = singleBook;
      }
      
      
      Object onActionFromRemoveBook(String bookName)
      {
              purchaseItemBasketAS.getPurchaseItemBasket().remove(bookName);
              return null;
      }
      
      @OnEvent (value="submit", component="purchaseInputForm")
      Object onFromSubmit()
      {
              String pCustomer = getUser().getUserName();
              String pSeller = "Book Store web -Seller";

              Customer customer = null;
              Seller seller = null;
              
              try
              {
                      customer = customers.retrieveSingleCustomer(pCustomer);
                      seller = sellers.retrieveSingleSeller(pSeller);
              }
              catch(Exception ex)
              {
                      viewErrorPage.setPassedMessage(ex.toString());
                      return viewErrorPage;
              }

              if (customer == null)
              {
                      viewErrorPage.setPassedMessage("Greska u prepoznavanju objekta Customer u Map-i");
                      return viewErrorPage;                   
              }
              if (seller == null)
              {
                      viewErrorPage.setPassedMessage("Greska u prepoznavanju objekta Seller u Map-i");
                      return viewErrorPage;                   
              }

              Purchase pb = new PurchaseBean();
              pb.executePurchaseCreation(customer, seller, purchaseItemBasketAS, purchasesItems, purchases);
              
              purchaseItemBasketAS = null;
              return viewPurchasePage;
      }

}